package com.example.library.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.library.common.PageResult;
import com.example.library.common.Result;
import com.example.library.dto.BaseQueryRequest;
import com.example.library.dto.BookQueryRequest;
import com.example.library.entity.Book;
import com.example.library.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 图书控制器
 *
   */
@Slf4j
@RestController
@RequestMapping("/api/books")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    /**
     * 分页查询图书列表
     */
    @GetMapping
    public PageResult<Book> getBookPage(BookQueryRequest queryRequest) {
        IPage<Book> page = bookService.getBookPage(queryRequest);
        return PageResult.success(page);
    }

    /**
     * 根据分类查询图书
     */
    @GetMapping("/category/{categoryId}")
    public PageResult<Book> getBooksByCategory(@PathVariable Long categoryId, BaseQueryRequest queryRequest) {
        IPage<Book> page = bookService.getBooksByCategory(categoryId, queryRequest);
        return PageResult.success(page);
    }

    /**
     * 搜索图书
     */
    @GetMapping("/search")
    public PageResult<Book> searchBooks(@RequestParam String keyword, BaseQueryRequest queryRequest) {
        IPage<Book> page = bookService.searchBooks(keyword, queryRequest);
        return PageResult.success(page);
    }

    /**
     * 获取热门图书
     */
    @GetMapping("/popular")
    public Result<List<Book>> getPopularBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getPopularBooks(limit);
        return Result.success(books);
    }

    /**
     * 获取最新图书
     */
    @GetMapping("/latest")
    public Result<List<Book>> getLatestBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getLatestBooks(limit);
        return Result.success(books);
    }

    /**
     * 获取推荐图书
     */
    @GetMapping("/recommended")
    public Result<List<Book>> getRecommendedBooks(@RequestParam(defaultValue = "10") Integer limit) {
        List<Book> books = bookService.getRecommendedBooks(limit);
        return Result.success(books);
    }

    /**
     * 根据ID获取图书信息
     */
    @GetMapping("/{id}")
    public Result<Book> getBookById(@PathVariable Long id) {
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    /**
     * 根据ISBN获取图书信息
     */
    @GetMapping("/isbn/{isbn}")
    public Result<Book> getBookByIsbn(@PathVariable String isbn) {
        Book book = bookService.findByIsbn(isbn);
        return Result.success(book);
    }

    /**
     * 创建图书
     */
    @PostMapping
    public Result<Void> createBook(@Valid @RequestBody Book book) {
        boolean success = bookService.createBook(book);
        return success ? Result.success("图书创建成功") : Result.error("图书创建失败");
    }

    /**
     * 更新图书
     */
    @PutMapping("/{id}")
    public Result<Void> updateBook(@PathVariable Long id, @Valid @RequestBody Book book) {
        book.setId(id);
        boolean success = bookService.updateBook(book);
        return success ? Result.success("图书更新成功") : Result.error("图书更新失败");
    }

    /**
     * 删除图书
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteBook(@PathVariable Long id) {
        boolean success = bookService.deleteBook(id);
        return success ? Result.success("图书删除成功") : Result.error("图书删除失败");
    }

    /**
     * 批量删除图书
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteBooks(@RequestBody List<Long> bookIds) {
        boolean success = bookService.batchDeleteBooks(bookIds);
        return success ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }

    /**
     * 更新图书状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateBookStatus(@PathVariable Long id, @RequestParam Integer status) {
        boolean success = bookService.updateBookStatus(id, status);
        return success ? Result.success("状态更新成功") : Result.error("状态更新失败");
    }

    /**
     * 批量更新图书状态
     */
    @PutMapping("/batch/status")
    public Result<Void> batchUpdateBookStatus(@RequestBody List<Long> bookIds, @RequestParam Integer status) {
        boolean success = bookService.batchUpdateBookStatus(bookIds, status);
        return success ? Result.success("批量状态更新成功") : Result.error("批量状态更新失败");
    }

    /**
     * 更新图书库存
     */
    @PutMapping("/{id}/stock")
    public Result<Void> updateBookStock(@PathVariable Long id,
                                        @RequestParam Integer totalCount,
                                        @RequestParam Integer availableCount) {
        boolean success = bookService.updateBookStock(id, totalCount, availableCount);
        return success ? Result.success("库存更新成功") : Result.error("库存更新失败");
    }

    /**
     * 检查图书是否可借
     */
    @GetMapping("/{id}/available")
    public Result<Boolean> isBookAvailable(@PathVariable Long id) {
        boolean available = bookService.isBookAvailable(id);
        return Result.success(available);
    }

    /**
     * 获取库存不足的图书
     */
    @GetMapping("/low-stock")
    public Result<List<Book>> getLowStockBooks(@RequestParam(defaultValue = "5") Integer threshold) {
        List<Book> books = bookService.getLowStockBooks(threshold);
        return Result.success(books);
    }

    /**
     * 获取图书统计信息
     */
    @GetMapping("/statistics")
    public Result<Object> getBookStatistics() {
        Object statistics = bookService.getBookStatistics();
        return Result.success(statistics);
    }

    /**
     * 检查ISBN是否存在
     */
    @GetMapping("/check/isbn")
    public Result<Boolean> checkIsbn(@RequestParam String isbn) {
        boolean exists = bookService.existsByIsbn(isbn);
        return Result.success(exists);
    }

}